#!/usr/bin/python

import os , time , sys , threading
from termcolor import colored , cprint
from sh import tail

os.chdir( os.path.dirname(os.path.realpath(__file__)) )

results_log = '../server/logs/results.log'
groups_log = '../server/logs/groups.log'
print_red_on_cyan = lambda x: cprint(x, 'red', 'on_cyan')
results_padding = [0,22,65,130]
groups_padding = [0,22,65,130]

################################################################################

def padd(parts,arr,num):
	if num == 0 : return ''
	start = arr[num-1] + len(parts[num-1])
	return ' ' * (arr[num] - start)
	
################################################################################

def print_line_results(parts):
        print ("%s%s%s%s%s%s%s" % (
                colored(parts[0] , 'yellow') ,
		padd(parts,results_padding,1) ,
                colored(parts[1].strip() , 'green') ,
                padd(parts,results_padding,2) ,
                colored(parts[2].strip() , 'blue') ,
                padd(parts,results_padding,3) ,
                colored(parts[3].strip() , 'red' , attrs=['bold'])))


def print_line_groups(parts):
	colors = ['yellow','green','yellow','blue','red','green','yellow']
	str = ''
	for i in range(len(parts)):
		str += padd(parts,groups_padding,i)
		str += colored(parts[i].rstrip() , colors[i])
	print str

################################################################################

def cycle():
	os.system("tput clear")

	print_red_on_cyan('RESULTS:')
	print ""
	print_line_results(["DATE","GROUP","KEY","FAILED ATTRIBUTE"])
	print_line_results(["---------","------------","-------------","----------------"])
	for line in tail("-20", results_log , _iter=True):		
		parts = line.split("\t")
		try :
			print_line_results(parts)
		except : pass

        print_red_on_cyan('LOG:')
	print ""
        print_line_groups(["DATE","GROUP",""])
        print_line_groups(["---------","------------","-------------"])
        for line in tail("-20", groups_log , _iter=True):
                parts = line.split("\t")
		try:
			print_line_groups(parts)
		except : pass
	os.system("tput cup 1255 0")

#################################################################################


while True:
#	threading.Thread(target=cycle)
	cycle()
	time.sleep(2)




